{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "y_ohotn_qybF"
   },
   "source": [
    "\n",
    "# **Instructions**:\n",
    "\n",
    "### 1 - Upload this notebook on [Google Colab](https://colab.research.google.com/) (this notebook need to run on Google Colab).\n",
    "### 2 - Open the notebook on Google Colab.\n",
    "### 3 - Change the Google Colab runtime to \"GPU\" by navigating to \"Runtime\" > \"Change runtime type\"\n",
    "### 4 - Run all cells to start training your model.\n",
    "### 5 - After the last command finish its execution, copy the file ```yolov4-tiny/training/yolov4-tiny-custom_last.weights``` from your google drive to the cloned project in the same folder as the 4_yolo_opencv_detector notebook.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "sLELVfEKDnI_"
   },
   "outputs": [],
   "source": [
    "!git clone https://github.com/AlexeyAB/darknet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "UcHcVVfHRqx5"
   },
   "outputs": [],
   "source": [
    "%cd ..\n",
    "from google.colab import drive\n",
    "drive.mount('/content/gdrive')\n",
    "\n",
    "!ln -s /content/gdrive/My\\ Drive/ /mydrive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "fHdLgyNRqP1X"
   },
   "outputs": [],
   "source": [
    "!ls /mydrive/yolov4-tiny"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "vRx_2ufLYKzq"
   },
   "outputs": [],
   "source": [
    "%cd /content/darknet/\n",
    "!sed -i 's/OPENCV=0/OPENCV=1/' Makefile\n",
    "!sed -i 's/GPU=0/GPU=1/' Makefile\n",
    "!sed -i 's/CUDNN=0/CUDNN=1/' Makefile\n",
    "!sed -i 's/CUDNN_HALF=0/CUDNN_HALF=1/' Makefile\n",
    "!sed -i 's/LIBSO=0/LIBSO=1/' Makefile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "YgWuHt3GK3Gz"
   },
   "outputs": [],
   "source": [
    "!make"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "PJ12EpatuOdw"
   },
   "outputs": [],
   "source": [
    "%cd data/\n",
    "!find -maxdepth 1 -type f -exec rm -rf {} \\;\n",
    "%cd ..\n",
    "\n",
    "%rm -rf cfg/\n",
    "%mkdir cfg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "eUeBO88DuSdj"
   },
   "outputs": [],
   "source": [
    "!cp /mydrive/yolov4-tiny/obj.zip ../\n",
    "!unzip ../obj.zip -d data/\n",
    "\n",
    "!cp /mydrive/yolov4-tiny/yolov4-tiny-custom.cfg ./cfg\n",
    "!cp /mydrive/yolov4-tiny/obj.names ./data\n",
    "!cp /mydrive/yolov4-tiny/obj.data  ./data\n",
    "!cp /mydrive/yolov4-tiny/process.py ./\n",
    "!cp /mydrive/yolov4-tiny/yolov4-tiny.conv.29 ./"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "N8JDSBpwusXn"
   },
   "outputs": [],
   "source": [
    "!python process.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "omzY915VqWS8"
   },
   "outputs": [],
   "source": [
    "!ls data/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "iblRcnnSu1Uq"
   },
   "outputs": [],
   "source": [
    "!./darknet detector train data/obj.data cfg/yolov4-tiny-custom.cfg yolov4-tiny.conv.29 -dont_show"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "gpuType": "T4",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
